package com.DeviceTest;

import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

import java.util.List;

import com.DeviceTest.helper.ControlButtonUtil;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;

/**
 * @author LanBinYuan
 * @date 2011-06-11
 * 
 */

public class LightsensorTestActivity extends Activity
        implements SensorEventListener {
    private static final String TAG = "LightsensorTestActivity";

    private TextView txt_Light;
    private TextView txt_sensor_info;
    private TextView txt_sensor_event;

	/** Called when the activity is first created. */
	private SensorManager sensorManager;
	
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);

		setTitle(getTitle() + "----("
				+ getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS) + ")");
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
		
		setContentView(R.layout.lightsensortest);
        initView();
		ControlButtonUtil.initControlButtonView(this);
		sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);

	}

    private void initView() {
        txt_Light = (TextView) findViewById(R.id.Light);
        txt_Light.setTextColor(android.graphics.Color.RED);
        txt_sensor_info = (TextView) findViewById(R.id.txt_sensor_info);
        txt_sensor_event = (TextView) findViewById(R.id.txt_sensor_event);
        txt_sensor_event.setTextColor(android.graphics.Color.GREEN);
    }

	protected void onResume() {
		super.onResume();

        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_LIGHT);
        if (null != sensors) {
            for (Sensor s : sensors) {
                Log.v(TAG, "registerListener " + s.getName());
                //updateSensorInfoText(s);
                sensorManager.registerListener(this, s,
                        SensorManager.SENSOR_DELAY_NORMAL);
                break;
            }
        }
	}

	//
	
	protected void onStop() {
		super.onStop();
        Log.v(TAG, "unregisterListener");
        sensorManager.unregisterListener(this);
	}

	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.dispatchKeyEvent(event);
	}

    private void updateSensorInfoText(Sensor sensor) {
        StringBuilder sb = new StringBuilder();
        sb.append("name:" + sensor.getName() + "\n");
        sb.append("vendor:" + sensor.getVendor() + "\n");
        sb.append("version:" + sensor.getVersion() + "\n");
        sb.append("maxRange:" + sensor.getMaximumRange() + "\n");
        sb.append("resolution:" + sensor.getResolution() + "\n");
        sb.append("power:" + sensor.getPower() + "\n");
        txt_sensor_info.setText(sb.toString());
    }

    @Override
    public void onSensorChanged(SensorEvent e) {
        Sensor sensor = e.sensor;
        switch (sensor.getType()) {
            case Sensor.TYPE_LIGHT: {
                updateSensorInfoText(sensor);
                String event = "Light:" + e.values[SensorManager.DATA_X];
                //Log.v(TAG, "onSensorChanged" + event);
                txt_sensor_event.setText(event);
            }
            default:
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
    }
}
